﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IRealize;
using Entity;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Dapper;

namespace Realize
{
    public class Users : IUsers
    {
        public User Login(string UserName, string Pwd,string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {

                string sql = $"select * from users where u_name = '{UserName}' and u_password = '{Pwd}'";
                List<User> list = con.Query<User>(sql).ToList();
                if (list.Count > 0)
                {
                    return list[0];
                }
                else
                {
                    return null;
                }
            }
        }

        

        public async Task<List<User>> Server(string sqlserver)
        {
            Task<List<User>> task = Task<List<User>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from users";
                    return con.Query<User>(sql).ToList();
                }
            });
            return await task;
        }
        public async Task<List<UserRoles>> Select2(int u_id,string sqlserver)
        {
            Task<List<UserRoles>> task = Task<List<UserRoles>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from UserRoles where UserID = @UserID";
                    return con.Query<UserRoles>(sql, new { UserID = u_id }).ToList();
                }
            });
            return await task;
        }
        public async Task<List<RolesJurisdiction>> Select3(int r_id,string sqlserver)
        {
            Task<List<RolesJurisdiction>> task = Task<List<RolesJurisdiction>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $@"select * from dbo.RolesJurisdiction rj inner join 
                                    dbo.Jurisdiction j on rj.JuriID = j.JuriID inner join 
                                    dbo.Roles r on rj.RolesID = r.RolesID where rj.RolesID =  @RolesID";
                    return con.Query<RolesJurisdiction>(sql, new { RolesID =r_id }).ToList();
                }
            });
            return await task;
        }
        public async Task<List<Jurisdiction>> Select4(int j_id,string sqlserver)
        {
            Task<List<Jurisdiction>> task = Task<List<Jurisdiction>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from Jurisdiction where JuriID = @JuriID";
                    return con.Query<Jurisdiction>(sql, new { JuriID = j_id }).ToList();
                }
            });
            return await task;
        }
    }
}
